"""Test Secure Controller"""
from pydive.lib.base import BaseController
from tg import expose, flash , require,redirect,request
from tg import session
from tg.decorators import validate
from repoze.what import predicates
from pylons.i18n import ugettext as _
from repoze.what.predicates import has_permission
from pydive.model import DBSession, metadata
from pydive.model.dive import Diver
from pydive.model.auth import User,Group
from pylons.i18n import ugettext as _
from sqlalchemy.orm import eagerload
import pprint
import formencode
from formencode import validators
from zope.sqlalchemy.datamanager import mark_changed


class ProfileController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    
    msg=_("You don't have a permission to view profile")
    
    @require(predicates.has_permission('profile', msg=msg))
    @expose(template="pydive.templates.profile.profile")
    def index(self):
        return dict(page='profile',diver=session['user'].diver[0])
    @require(predicates.has_permission('profile', msg=msg))
    @expose(template="pydive.templates.profile.edit")
    def edit(self):
        #u = DBSession.query(User).options(eagerload('diver')).filter(User.user_name == userid).all()[0]
        #diver = DBSession.query(Diver).options(eagerload('address')).options(eagerload('contact')).filter(User.user_name == "macio").filter(Diver.id == User.diver_id).first() 
        return dict(page='profile')
    
    @require(predicates.has_permission('profile', msg=msg))
    @expose(template="pydive.templates.profile.equipment")
    def equipment(self):
        return dict(page='equipment')
    
    @require(predicates.has_permission('new_profile', msg=_('No access here')))
    @expose(template="pydive.templates.profile.new")
    def new(self, **kw):
        #flash(pprint.pformat(kw))
        return dict(page='new')
    
    @require(predicates.has_permission('profile', msg=msg))
    @expose('json')
    def details(self):
        ###tu cos trzeba zrobic
        return dict(diver=session['user'].diver[0])
    
    
        
    #@require(predicates.has_permission('profile', msg=msg))
    @expose()
    def save(self,**kw):
        flash(pprint.pformat(kw))
        redirect('/')
        
    @require(predicates.has_permission('new_profile', msg=_('No access here')))
    @expose()
    @validate(validators={"birthdate":validators.DateConverter(month_style='dd/mm/yyyy')},error_handler=new)
    def create(self,firstname,middlename,lastname,birthdate):
        diver = Diver()
        diver.firstname = firstname
        diver.middlename = middlename
        diver.lastname = lastname
        diver.birthdate = birthdate
        userid = request.identity['repoze.who.userid']
        u = DBSession.query(User).filter(User.user_name == userid).all()[0]
        diver.user = u
        DBSession.save(diver)
        u.groups.remove(DBSession.query(Group).filter(Group.group_name == 'new_users').all()[0])
        u.groups.append(DBSession.query(Group).filter(Group.group_name == 'users').all()[0])
        DBSession.update(u)
        session.save()
        u.diver
        session['user'] = u
        #mark_changed(DBSession())
        flash("Your profile has been created")
        redirect('/profile')

   